/***************************************************************************
 *   Copyright (C) 2009 by Marian Skvarek   *
 *   skvarek.marian@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A TrackICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "CGraphicsView.h"
#include "CGlobals.h"

#include <QWheelEvent>

namespace easyscore
{

	namespace gui
	{

		CGraphicsView::CGraphicsView()
		{
			mCurrentZoom = 1;
			mCtrlPressed = false;
			setInteractive ( true );
			
			double defaultZoom = easyscore::globals::CGlobals::getDefaultZoomFactor();
                        scale(defaultZoom,defaultZoom);
		}


		CGraphicsView::~CGraphicsView()
		{
		}


                void CGraphicsView::zoom(int steps)
                {
                    if ( (mCurrentZoom < 0.5  && steps < 0) || ( mCurrentZoom > 2 && steps > 0 ) )
                    {
                            return;
                    }

                    qreal delta = steps;

                    if ( steps > 0 )
                            delta = 1 + ( delta / 1200 );
                    else
                            delta = 1/ ( 1 - ( delta / 1200 ) );

                    scale ( delta,delta );
                    mCurrentZoom *= delta;
                }

                void CGraphicsView::lock(bool lock)
                {
                    setInteractive(!lock);
                }


		void CGraphicsView::wheelEvent ( QWheelEvent* event )
		{
                    //QGraphicsView::wheelEvent(event);

                    if(mCtrlPressed)
                        zoom(event->delta());

                    //TODO - temp
                    //event->accept();
		}

		void CGraphicsView::keyPressEvent ( QKeyEvent * event )
		{
			QGraphicsView::keyPressEvent(event);
			
			if ( event->key() == Qt::Key_Control )
				mCtrlPressed = true;
		}

		void CGraphicsView::keyReleaseEvent ( QKeyEvent * event )
		{
			QGraphicsView::keyReleaseEvent(event);
			
			if ( event->key() == Qt::Key_Control )
				mCtrlPressed = false;
		}

	}

}
